package org.dromara.common.redis.handler

import cn.hutool.http.HttpStatus
import com.baomidou.lock.exception.LockFailureException
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.servlet.http.HttpServletRequest
import org.dromara.common.core.domain.R
import org.dromara.common.core.domain.R.Companion.fail
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

/**
 * Redis异常处理器
 *
 * @author LikeYouDo
 * @date 2025/7/13
 */
@RestControllerAdvice
class RedisExceptionHandler {

    companion object {
        private val log = KotlinLogging.logger {}
    }

    /**
     * 分布式锁Lock4j异常
     */
    @ExceptionHandler(LockFailureException::class)
    fun handleLockFailureException(e: LockFailureException, request: HttpServletRequest): R<Void?> {
        val requestURI = request.requestURI
        log.error { "${"获取锁失败了'{}',发生Lock4j异常."} $requestURI $e" }
        return fail<Void?>(HttpStatus.HTTP_UNAVAILABLE, "业务处理中，请稍后再试...")
    }
}
